/*
 * ScrollingBG.cpp
 *
 *  Created on: Feb 15, 2010
 *      Author: dcrown
 */


#include "ScrollingBG.h"
#include "..\Graphics\DXManager.h"
#include "..\Systems\Logger.h"
#include "..\Systems\BasicWindow.h"

extern DXManager* gpDX;
extern BasicWindow* gpBW;

#define MOVE_INC 1
#define TEXT_WIDTH 1024
#define TEXT_HEIGHT 512

void ScrollingBG::Create()
{
	screenWidth = gpBW->GetWidth();
	screenHeight = gpBW->GetHeight();

	texture = new Texture;
	texture->Create("resources\\textures\\numberedBG.bmp", "bg", TEXT_WIDTH, TEXT_HEIGHT);

	sprite1 = new Sprite;
	sprite2 = new Sprite;
	sprite1->Create("bg1", texture);
	sprite2->Create("bg2", texture);

	//maxOff = TEXT_WIDTH-screenWidth;
	offset = 0;

	//these values never change
	r1.top = r2.top = 0;
	r1.bottom = r2.bottom = screenHeight;
	r2.left = 0;

	Logger::getInstance().LOG(Logger::DEBUG, "ScrollingBG Created");
}

void ScrollingBG::Update()
{
	r1.left = offset;

	int overlap = (r1.left + screenWidth) - TEXT_WIDTH;
	if (overlap > 0)
	{
		r1.right = TEXT_WIDTH;
		r2.left = 0;
		r2.right = overlap;

		sprite1->SetTextureRect(&r1);
		sprite1->Update();
		sprite2->SetTextureRect(&r2);
		sprite2->TranslateOriginal(screenWidth-overlap, 0);
		sprite2->Update();
	}
	else
	{
		r1.right = r1.left+screenWidth;
		r2.right = 0;

		sprite1->SetTextureRect(&r1);
		sprite1->Update();
	}

	//Logger::getInstance().LOG(Logger::DEBUG, "1L=%d, 1R=%d, 2L=%d, 2R=%d", r1.left, r1.right, r2.left, r2.right);

	//increment offset for next pass
	offset = (offset+1)%TEXT_WIDTH;
}

void ScrollingBG::Render()
{
	//sprite 1 is always on the screen
	sprite1->Draw();

	//only draw sprite 2 if necessary
	if (r2.right > 0)
	{
		sprite2->Draw();
	}
}
